<?php
/**************
* DESCRIPTION *
***************
* Return or register the code of a file user
*
* Usage :
* -------
* call get() method to get the state
* call set() method to register the state
*
*/
	include_once "../library/DbConnection.php";
	include_once "../globals.php";
	include_once "../Debug.php";

	class Code
	{
		private $db;



		/****************
		** CONSTRUCTOR **
		****************/
		public function __construct()
		{
			session_start();
			$this->db = new DbConnection($GLOBALS["db_server"], $GLOBALS["db_login"], $GLOBALS["db_pass"], $GLOBALS["db_name"]);
			$this->db->connect();
			 
		}



		/*******************
		** PUBLIC METHODS **
		*******************/

		/** Get the movement of the user
		* @return	{Object}	:	"movement" -> The movement of the user
		* 					"session"  -> Session state (true/false);
		**/
		public function get()
		{
			$ret		= array();
			if(!isset($_SESSION['user_id']))
				$ret["session"]	= false;
			else{
				$req				= $this->db->query("SELECT rememberCode FROM logout_Users WHERE id='".$_SESSION['virtual_id']."'");
				$res				= $this->db->fetch_object($req);
				$ret["session"]	= true;
				$ret["code"]		= unserialize($res->rememberCode);
			}
			return $ret;
		}

		/** Set the interruptors positions of the user
		* @param	{Object}	:	"positions" -> The state
		* @return	{Object}	:	"result" ::	true  -> Saved
		* 								false -> Error
		* 					"session" -> Session state (true/false);
		**/
		public function set($code)
		{
			$ret		= array();
			if(!isset($_SESSION['user_id']))
				$ret["session"]	= false;
			else{
				$res				= $this->db->query("UPDATE logout_Users SET rememberCode='".serialize($code)."' WHERE id='".$_SESSION['virtual_id']."'");
				$ret["session"]	= true;
				$ret["result"]		= $res;
			}
			return $ret;
		}
	}
?>